﻿using System.Text;
using System.Web.Mvc;
using Kratos.Framework.ContentManagement.Models.SystemRights;
using Kratos.Framework.ContentManagement.ViewModels;
using Kratos.Framework.Infrastructures;
using Kratos.Framework.Modules.Core.Services;
using System.ComponentModel.DataAnnotations;

namespace Kratos.Framework.Modules.Core.Controllers
{
    /// <summary>
    /// 控制器基类,所有控制器都应该继承与它,但也可以自己定义一个来继承
    /// </summary>
    [HandleError]
    //[KratosSecurity]
    //[HibernateSession]
    public abstract class KratosController : Controller
    {
        protected UserInfo CurrentUser
        {
            get { return DependencyResolver.Current.GetService<IUserInfoServices>().CurrentUser; }
        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext.Exception != null)
            {
                string msgTmp;
                if (filterContext.Exception is ValidationException)//如果是验证类异常 ，就不出详细异常信息了
                    msgTmp = "<b>验证错误:  </b>{0}";
                else
                    msgTmp = "<b>异常消息:  </b>{0}</p><b>触发Action:  </b>{1}</p><b>异常类型:  </b>{2}";
                var excResult = new KratosJsonResult();
                excResult.Data = new ExtResult
                {
                    Success = false,
                    Msg = string.Format(msgTmp,
                            filterContext.Exception.GetBaseException().Message,
                            filterContext.ActionDescriptor.ActionName,
                            filterContext.Exception.GetBaseException().GetType().ToString())
                };
                filterContext.Result = excResult;
                filterContext.ExceptionHandled = true;
            }
        }

        #region KratosJsonResult
        protected internal KratosJsonResult KratosJson(object data)
        {
            return KratosJson(data, null /* contentType */, null /* contentEncoding */, JsonRequestBehavior.DenyGet);
        }

        protected internal KratosJsonResult KratosJson(object data, string contentType)
        {
            return KratosJson(data, contentType, null /* contentEncoding */, JsonRequestBehavior.DenyGet);
        }

        protected internal virtual KratosJsonResult KratosJson(object data, string contentType, Encoding contentEncoding)
        {
            return KratosJson(data, contentType, contentEncoding, JsonRequestBehavior.DenyGet);
        }

        protected internal KratosJsonResult KratosJson(object data, JsonRequestBehavior behavior)
        {
            return KratosJson(data, null /* contentType */, null /* contentEncoding */, behavior);
        }

        protected internal KratosJsonResult KratosJson(object data, string contentType, JsonRequestBehavior behavior)
        {
            return KratosJson(data, contentType, null /* contentEncoding */, behavior);
        }

        protected internal virtual KratosJsonResult KratosJson(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            return new KratosJsonResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding,
                JsonRequestBehavior = behavior
            };
        }
        #endregion
    }
}
